#pragma once

#include <iostream>
#include <string>
#include "Socket.hpp"

namespace TcpServerModule
{
    using namespace SocketModule;
    
// 他只负责进行IO的紧口型不对协议进任何的管制
    class TcpServer
    {
    public:
        TcpServer(int port):_listen_sock(std::make_unique<TcpSocket>())
        {
            _listen_sock->BuildTcpSocketMethod(port); 
        }

        ~TcpServer()
        {
            _listen_sock->Close();
        }

        void loop()
        {
            _running = true;
            while (_running)
            {
                // 1. accept
                auto sockfd = _listen_sock->Acceptor(&clientaddr);

                if (sockfd == nullptr) continue;

                // 2. IO deal

                LOG(LogLevel::DEBUG) << "get a new client info is: " << client.addr.Ip() << ":" << client.addr.Port();

            }
            _running = false;
        }

    private:
        //  一定需要有一个listen sock
        std::unique_ptr<SocketModule::Socket> _listen_sock;

        bool quit = false;
        bool _running;
    };

}